package com.skystar.commons.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.skystar.commons.basedata.BaseDataJson;
import com.skystar.commons.hmenum.ResultCodeEnum;

@RestControllerAdvice
public class GlobalException {

	private final static Logger logger = LoggerFactory.getLogger(GlobalException.class);

	/**
	 * 
	 * @方法名: handle
	 * @描述: TODO
	 * @param e
	 * @return BaseDataJson<Object>
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public BaseDataJson<Object> handle(Exception e) {
		if (e instanceof ServiceException) {
			ServiceException serviceException = (ServiceException) e;
			
			return new BaseDataJson<>(serviceException.getErrorCode(),serviceException.getMessage());
		} else {
			logger.error("【系统异常】{}", e);
			return new BaseDataJson<>(ResultCodeEnum.SYSTEMERROR);
		}
	}
}
